package com.googlecode.spreadit.bean;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.util.Log;

import com.googlecode.spreadit.matching.MatchExpression;

/**
 * A {@link SpreadRule} identifies the target of the information and the
 * condition under which the rule is executed.
 * 
 * @author Markus L. Dechert (markus.dechert@googlemail.com)
 * 
 */
public class SpreadRule implements Serializable {

	private final String LOG_TAG = getClass().getSimpleName();

	public static final String ATTRIBUTE_ID = "com.googlecode.spreadit.bean.SpreadRule.ID";
	public static final String ATTRIBUTE_NAME = "com.googlecode.spreadit.bean.SpreadRule.NAME";
	public static final String ATTRIBUTE_ACTIVE = "com.googlecode.spreadit.bean.SpreadRule.ACTIVE";
	public static final String ATTRIBUTE_TARGET_CHANNEL = "com.googlecode.spreadit.bean.SpreadRule.TARGET_CHANNEL";

	/**
	 * Generated serialization ID.
	 */
	private static final long serialVersionUID = -6629327709358815083L;

	private Long id;
	private String name;
	private boolean active;
	private List<SpreadReceiver> targetReceivers;

	private MatchExpression matchIf;

	public SpreadRule() {
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isActive() {
		return active;
	}

	public void setActive(boolean active) {
		this.active = active;
	}

	public List<SpreadReceiver> getTargetReceivers() {
		return targetReceivers;
	}

	public void setTargetReceivers(List<SpreadReceiver> targetReceivers) {
		this.targetReceivers = targetReceivers;
	}

	/**
	 * Convenience method to add a single {@link SpreadReceiver target receiver}
	 * to the {@link #targetReceivers List of target receivers}. If the
	 * {@link #targetReceivers} attribute has not been initialized with a list
	 * yet this is done in this method.
	 * 
	 * @param targetReceiver
	 *            the element that should be added to the list.
	 *            <code>null</code> is not added but an error is logged.
	 * @return the list after insertion.
	 */
	public List<SpreadReceiver> addTargetReceiver(SpreadReceiver targetReceiver) {

		if (targetReceiver != null) {

			if (this.targetReceivers == null) {
				this.targetReceivers = new ArrayList<SpreadReceiver>();
			}
			this.targetReceivers.add(targetReceiver);
		} else {
			Log.e(LOG_TAG,
					"targetReceiver must not be null! No element added to targetReceivers list.");
		}

		return getTargetReceivers();
	}

	/**
	 * Returns the {@link #targetReceivers List of target receivers} in a
	 * {@link String} in the form
	 * 
	 * <pre>
	 * Peter Pan (E-Mail)
	 * Fairy Lady (SMS)
	 * Hannibal (E-Mail)
	 * </pre>
	 * 
	 * @return the displayable list.
	 */
	public String getTargetReceiversAsString() {
		StringBuffer result = new StringBuffer("");

		for (SpreadReceiver receiver : this.targetReceivers) {
			result.append(receiver.getName() + " ("
					+ receiver.getTargetChannel() + ")\n");
		}
		// don't end with a line break
		final int l = result.length();
		if (l > 2) {
			result.delete(l - 1, l);
		}

		return result.toString();
	}

	public MatchExpression getMatchIf() {
		return matchIf;
	}

	public void setMatchIf(MatchExpression matchIf) {
		this.matchIf = matchIf;
	}

	/**
	 * This method returns the name of the rule.
	 */
	@Override
	public String toString() {
		return getName();
	}

}
